﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Engine.Utilities;

namespace Project.Module.DAO.BL
{
    public class FeedBackBL : IBLTable<Feedback, int>
    {
        public void InsertLienHe(string name, string content)
        {
            int id = GetMaxId() + 1;
            DateTime date = DateTime.Now;

            string sql = @"insert into Feedback (
                              FeedbackID, CustomerID, Type, Content,
                              CreatedBy, CreatedDate, ModifiedDate, ModifiedBy
                           ) values (
                              {0}, Cast('1' as int), null, {1},  
                              {2}, {3}, null, null
                           )";
            var query = this.Context.ExecuteQuery<FeedBackModel>(sql, new object[] { id, content, name, date }).ToList();
        }

        public int GetMaxId()
        {
            int Id = 0;
            var query = from e in this.Context.Feedbacks
                        orderby e.FeedbackID descending
                        select new { Id = e.FeedbackID };

            if (query != null)
            {
                Id = ConvertUtil.ToInt32(query.Select(c => c.Id).FirstOrDefault().ToString());
            }

            // return
            return Id;
        }

        public List<FeedBackModel> GetCommentEvent(int eventId) {
            var query = from e in this.Context.Feedbacks
                        where e.EventID == eventId
                        select new FeedBackModel() { 
                            Content = e.Content,
                            CreatedBy = e.CreatedBy,
                        };

            if (query != null) {
                return query.ToList();
            }

            return null;
        }

        public List<FeedBackModel> GetCommentProduct(int productId, int cateId)
        {
            var query = from e in this.Context.Feedbacks
                        join a in this.Context.Products
                        on e.ProductID equals a.ProductID
                        where e.ProductID == productId && a.CategoryID == cateId
                        select new FeedBackModel()
                        {
                            Content = e.Content,
                            CreatedBy = e.CreatedBy,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }

        public List<FeedBackModel> GetAllComment() {
            var query = from e in this.Context.Feedbacks
                        select new FeedBackModel()
                        {
                            CreatedDate = e.CreatedDate != null ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            Content = e.Content,
                            CreatedBy = e.CreatedBy,
                            ProductId = e.ProductID != null ? e.ProductID.Value : 0,
                            EventId = e.EventID != null ? e.EventID.Value : 0,
                            FeedbackID = e.FeedbackID,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }

        public void DeleteComment(int feedbackId) {
            Feedback feed = this.Context.Feedbacks.Single(c => c.FeedbackID == feedbackId);
            this.Context.Feedbacks.DeleteOnSubmit(feed);
            this.Context.SubmitChanges();
        }

        public List<FeedBackModel> GetTopComment(int limit) {
            var query = from e in this.Context.Feedbacks
                        where e.ProductID != null
                        select new 
                        {
                            ProductId = e.ProductID != null ? e.ProductID.Value : 0,
                            FeedbackID = e.FeedbackID,
                            count = 1,
                        };

            var queryGroup = from e in query
                             where e.ProductId != null || e.ProductId != 0
                             group e by new { e.ProductId }
                                 into g
                                 select new FeedBackModel() {
                                     ProductId = g.Key.ProductId,
                                     FeedbackID = g.Sum(c => c.count),
                                 };

            if (queryGroup != null)
            {
                return queryGroup.Take(limit).ToList();
            }

            return null;
        }
    }
}
